import {defineStore} from "pinia";
import axios from "axios";

export const useProductStore = defineStore('product', {
    state: () => ({
        allProducts: [], // 商品页面获取的商品信息
        products: [], // 用户发布的商品信息
        productStats: [], // 商品的数据
        loading: false,
        error: null,
    }),
    actions: {
        async fetchAllProducts() {
            try {
                const response = await axios.post('/api/products/allProList');
                if (response.data.code === 200) {
                    console.log(response.data.data)
                    this.allProducts = response.data.data;
                    return true;
                }
            } catch (error) {
                this.error = error.response ? error.response.data.message : '获取所有商品列表失败';
            } finally {
                this.loading = false;
            }
            return false;
        },
        async fetchProducts(userId, type = 'full') {
            if (!userId) {
                this.error = "用户ID不能为空";
                return false;
            }
            this.loading = true;
            this.error = null;
            try {
                const response = await axios.post('/api/products/proList', {
                    userId: userId,
                    type: type,
                });
                if (response.data) {
                    console.log(response.data.data)
                    if (type === 'full') {
                        this.products = response.data.data;
                    } else if (type === 'chart') {
                        this.productStats = response.data.data;
                    }
                    return true;
                }
            } catch (error) {
                this.error = error.response ? error.response.data.message : '获取商品列表失败';
            } finally {
                this.loading = false;
            }
            return false;
        },
        async publishProducts(formState, userId) {
            this.loading = true;
            this.error = null;
            try {
                const response = await axios.post('/api/products/publish', {
                    userId: userId,
                    price: formState.price,
                    name: formState.name,
                    descStatus: formState.descStatus,
                    description: formState.description
                    // 图片路径
                });
                if (response.data) {
                    console.log(response.data);
                    return true;
                }
            } catch (error) {
                this.error = error.response ? error.response.data.message : '发布商品失败';
            } finally {
                this.loading = false;
            }
            return false;
        },
        async deleteProducts(product) {
            this.loading = true;
            this.error = null;
            try {
                const response = await axios.post('/api/products/delete', {
                    productId: product.productId,
                });
                if (response.data) {
                    const productIndex = this.products.findIndex(p => p.productId === product.productId);
                    if (productIndex !== -1) {
                        this.products.splice(productIndex, 1); // 删除该商品
                    }
                    return true;
                }
            } catch (error) {
                this.error = error.response ? error.response.data.message : '删除失败';
            } finally {
                this.loading = false;
            }
            return false;
        },
        async reviseProducts(product) {
            this.loading = true;
            this.error = null;
            try {
                const response = await axios.post('/api/products/revise', {
                    productId: product.productId,
                    price: product.price,
                    name: product.name,
                    descStatus: product.descStatus,
                });
                if (response.data) {
                    const index = this.products.findIndex(p => p.productId === product.productId);
                    if (index !== -1) {
                        this.products[index] = {
                            productId: product.productId,
                            price: product.price,
                            name: product.name,
                            descStatus: product.descStatus,
                        }
                    }
                    return true;
                }
            } catch (error) {
                this.error = error.response ? error.response.data.message : '删除失败';
            } finally {
                this.loading = false;
            }
            return false;
        },

    },
    getters: {
        getProductById: (state) => (id) => {
            return state.allProducts.find(product => product.productId === id) || {};
        }
    }
});
